using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.XLANGs.BaseTypes;
using MapTestFramework.Common.Tests.Transforms;

namespace MapTestFramework.Common.Tests
{
    [TestClass]
    public class MappingFixtureFixture : MappingFixture
    {
        #region Constructor And Initializing Members

        public MappingFixtureFixture()
        {
            return;
        }

        #endregion

        #region Additional test attributes

        // [ClassInitialize()]
        // public static void ClassInitialize(TestContext testContext) { }
        // [ClassCleanup()]
        // public static void ClassCleanup() { }
        // [TestInitialize()]
        // public void TestInitialize() { }
        // [TestCleanup()]
        // public void TestCleanup() { }

        #endregion

        #region MappingFixtureEx Members

        protected override string SourcePathBase
        {
            get { return @"Schema1.xml"; }
        }
        protected override string ExpectedPathBase
        {
            get { return @"Schema2.xml"; }
        }

        protected override IMapExecuter CreateMap()
        {
            return new TestableMapBaseMapExecuter(new Schema1_To_Schema2());
        }
        protected override void OnTestExecuting(object sender, TestEventArgs e)
        {
            const string xpathDateTime = @"/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Field_CurrentDate' and namespace-uri()='']";
            XmlHelper.UpdateNodes(xpathDateTime, 
                DateTime.Now.ToString("yyyyMMdd"), e.ExpectedPath, e.ExpectedPath
                );
            
            base.OnTestExecuting(sender, e);
            return;
        }
        protected override void OnTestExecuted(object sender, TestEventArgs e)
        {
            const string xpathDateTime = @"/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Field_CurrentDateTime' and namespace-uri()='']";
            DateTime fullDateTime = DateTime.Parse(
                XmlHelper.GetSingleNodeOrThrowException(xpathDateTime, e.OutputPath).InnerXml,
                CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal
                );
            Assert.IsTrue((fullDateTime >= e.StartDateTime), "DateTime value does not exceed StartDateTime");
            Assert.IsTrue((fullDateTime <= DateTime.Now), "DateTime value exceeds DateTime.Now");
            XmlHelper.RemoveNodes(xpathDateTime, e.OutputPath, e.OutputPath);

            const string xpathGuidNode = @"/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Field_Guid' and namespace-uri()='']";
            System.Guid dummy = new Guid(XmlHelper.GetSingleNodeOrThrowException(xpathGuidNode, e.OutputPath).InnerXml);
            XmlHelper.UpdateNodes(xpathGuidNode,
                "{C483351A-C038-49a5-954C-3AF00AE2D71A}", e.OutputPath, 
                e.OutputPath
                );
            
            base.OnTestExecuted(sender, e);
            return;
        }

        #endregion

        #region Test Members

        [TestMethod]
        public void TestExecuteBaseTest()
        {
            base.ExecuteBaseTest();

            Assert.IsTrue(File.Exists("Schema1TestExecuteBaseTest.xml"));
            Assert.IsTrue(File.Exists("Schema2TestExecuteBaseTest_Actual.xml"));
            Assert.IsTrue(File.Exists("Schema2TestExecuteBaseTest_Expected.xml"));
            return;
        }
        [TestMethod]
        public void TestExecuteMapTest()
        {
            MapTestCases collection = new MapTestCases(
                new string[] {
                    "/*[local-name()='Root' and namespace-uri()='http://MySampleProject.Schemas.Schema1']/*[local-name()='Record_OneToOne_Optional' and namespace-uri()='']",
                    "/*[local-name()='Root' and namespace-uri()='http://MySampleProject.Schemas.Schema1']/*[local-name()='Field_OneToMultiple' and namespace-uri()='']"
                    },
                new string[] {
                    "/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Record_OneToOne_Optional' and namespace-uri()='']",
                    "/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Field_OneToMultiple1' and namespace-uri()='']",
                    "/*[local-name()='Root2' and namespace-uri()='http://MySampleProject.Schemas.Schema2']/*[local-name()='Field_OneToMultiple2' and namespace-uri()='']"
                    }
                );
            collection.AddTestCase(
                new string[] { null, "NewValue" },
                new string[] { null, "NewValue", "NewValue" }
                );
            base.ExecuteMapTest(collection);

            Assert.IsTrue(File.Exists("Schema1TestExecuteMapTest.xml"));
            Assert.IsTrue(File.Exists("Schema2TestExecuteMapTest_Actual.xml"));
            Assert.IsTrue(File.Exists("Schema2TestExecuteMapTest_Expected.xml"));
            return;
        }

        #endregion
    }
}
